package com.wzapp.sitposture.common.util;

import com.wzapp.sitposture.common.base.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;

import java.util.Map;
import java.util.Set;

/**
 *
 * 结构：
 *     aiyige://主机名/模块名/页面名称?key1=value2&key2=values
 *
 * aiyige://everygod.com/home/detail_photo?momentId=xxxx&
 *
 * http://everygod.com/home/detail_photo?momentId=xxxx&
 *
 */
@Slf4j
public class RouterUtil {

    private static Environment env = SpringContextHolder.getBean("environment");

    private static final String DOMAIN_APP = env.getProperty("ql.domain.short.app");

    /**
     * 域名
     */
    private static final String DOMAIN_WAP = env.getProperty("ql.domain.short.wap");

    /**
     * 原生协议头
     */
    private static final String NATIVE_PREFIX = env.getProperty("ql.router.prefix.app");

    /**
     * H5协议头
     */
    private static final String WAP_PREFIX = env.getProperty("ql.router.prefix.wap");

    /**
     * 首页
     */
    public static final String MODEL_INDEX = "/index";
    /**
     * 学习页
     */
    public static final String MODEL_LEARN = "/learn";
    /**
     * 详情页
     */
    public static final String MODEL_DETAIL = "/detail";
    /**
     * 我的页
     */
    public static final String MODEL_MINE = "/center";
    /**
     * 工具
     */
    public static final String MODEL_TOOL = "/tool";

    private static final String PAGE_PATH_DETAIL = "/detail";

    private static final String PAGE_PREFIX_MINE = "/my_";

    private static final String PARAM_NAME_MOMENT = "momentId";

    /**
     * app识别
     */
    private static final String APP_HUIMOU= "huimou";


    /**
     * @param model
     * @param subject
     * @param momentId
     * @param params
     * @return
     */
    public static String genRouter(String model,String subject, String momentId, Map<String, String> params){
        if (ToolUtil.isOneEmpty(subject, momentId)) {
            return "";
        }

        StringBuffer sb = new StringBuffer();
        sb.append(model);

        sb.append("/").append(subject);

        sb.append("?").append(PARAM_NAME_MOMENT).append("=").append(momentId);
        if (ToolUtil.isNotEmpty(params) && params.size() > 0) {
            Set<String> keys = params.keySet();
            for (String key : keys) {
                sb.append("&").append(key).append("=").append(params.get(key));
            }
        }

        return sb.toString();
    }

    public static void main(String[] args) {
        String route = RouterUtil.genRouter(RouterUtil.MODEL_DETAIL, "article", "lskdlfaksl", null);
        System.out.println("-----------wzj-test-----------route值=" + route + "," + "当前类=RouterUtil.main()");
        String route2 = RouterUtil.genRouter(RouterUtil.MODEL_DETAIL, "video", "lskdlfkwelflk", null);
        System.out.println("-----------wzj-test-----------route2值=" + route2 + "," + "当前类=RouterUtil.main()");
    }
}
